
export class InputManager {
  events: any = {};
  constructor() {
    this.addClickListener();
  }
  on(event: string, callback: any) {
    // 给事件绑定回调函数
    if (!this.events[event]) {
      this.events[event] = [];
    }
    this.events[event].push(callback);
  }

  emit(event: string, data?: any) {
    const callbacks = this.events[event];
    if (callbacks) {
      callbacks.forEach((callback: (arg: any) => void) => {
        callback(data);
      });
    }
  }
  addClickListener() {
    const self = this;
    let eventSelect;
    // @ts-ignore
    if (window.navigator.msPointerEnabled) {
      eventSelect = "MSPointerDown";
    } else {
      eventSelect = "touchstart";
    }

    document.addEventListener(eventSelect, function (event: any) {
      event.stopPropagation();
      if (event.target.classList.contains("tile")) {
        // console.log('点击方块', event.target.classList[2]);
        let position = event.target.classList?.[2]
        if (position) {
          self.emit("select", { x: position.split('-')?.[2], y: position.split('-')?.[3] });
        }
      }
    });
  }
}